package com.daylive.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.daylive.model.Admin;
import com.daylive.model.Agent;
import com.daylive.model.Channel;
import com.daylive.page.Page;
import com.daylive.service.AdminService;
import com.daylive.service.AgentService;
import com.daylive.service.ChannelService;
import com.daylive.service.Const;
import com.daylive.util.StringUtil;


@Controller
@RequestMapping("channel")
public class ChannelController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private ChannelService  channelService;
	@Autowired
	private AgentService  agentService;
	private List<Agent>  agentList = null;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/channel_list.do")
	public ModelAndView getChannelList() {
		return getChannelList(1);
	}
	@RequestMapping("/channel_page_list.do")
	public ModelAndView getChannelList(@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("channel_list");
		HttpSession session = getSession();
		try {
		Admin admin = adminService.getAdminByAccount(String.valueOf(session.getAttribute(Const.SESSION_ADMIN_ACCOUNT)));
		if (admin.getAgentId()>0) {
			Page<Channel> pageList = channelService.getChannelList(admin.getAgentId(),pageNumber);
			mv.addObject("totalPage", pageList.getTotalPage());
			mv.addObject("totalCount", pageList.getTotalCount());
			mv.addObject("channelList", pageList.getResultList());
		}else {
			Page<Channel> pageList = channelService.getChannelList(0,pageNumber);
			mv.addObject("totalPage", pageList.getTotalPage());
			mv.addObject("totalCount", pageList.getTotalCount());
			mv.addObject("channelList", pageList.getResultList());
		}
		mv.addObject("currentPage", pageNumber);
		} catch (Exception e) {
			ModelAndView mv1 = new ModelAndView("error");
			mv1.addObject("msg", "查询数据库出错！");
		}
		return mv;
	}

	@RequestMapping("/channel_add.do")
	public ModelAndView addChannel() {
		ModelAndView mv = new ModelAndView();
		if (agentList == null || agentList.size() == 0) {
			this.agentList = agentService.getAgentList();
		}
		mv.addObject("agentList", agentList);
		mv.setViewName("channel_add");
		return mv;
	}

	@RequestMapping("/channel_save.do")
	public ModelAndView saveChannel(Channel channel) {
		ModelAndView mv = new ModelAndView();
		channel.setStatus(1);
		channel.setTime(StringUtil.getStrDate());
		int rel = channelService.addChannel(channel);
		mv.setViewName("success");
		mv.addObject("msg", rel > 0 ? "数据添加成功！" : "数据添加失败！");
		return mv;
	}
	 @RequestMapping("/channel_edit.do")
		public ModelAndView editChannel(@RequestParam("id")int id) {
		 Channel channel = channelService.getChannelById(id);
			ModelAndView mv = new ModelAndView();
			mv.addObject("channel", channel);
			mv.setViewName("channel_edit");
			return mv;
		}
	    @RequestMapping("/channel_update.do")
	   	public ModelAndView updateChannel(Channel channel) {
	   		ModelAndView mv = new ModelAndView();
	   		int rel =channelService.updateChannel(channel);
	   		mv.setViewName("success");
			mv.addObject("msg",rel > 0 ? "数据修改成功！" : "数据修改失败！");
			return mv;   		
	   	}
	@RequestMapping("/channel_del.do")
	public String deleteChannel(@RequestParam("id") int id) {
		channelService.delChannel(id);
		return "success";
	}

	@RequestMapping("/channel_delAll.do")
	public String deleteAll(String ids) {
		String[] array_IDS = ids.split(",");
		channelService.delAll(array_IDS);
		return "success";
	}
}